#!/usr/bin/python
#-*- coding:utf-8 -*-

import sys,time
if __name__ == '__main__':
    if(len(sys.argv) ==2 ):
        blocks = 0
        try:
            blocks = int(sys.argv[1])
        except ValueError:
            print( "usage: python eatmem.py <blocks to allocate,should be an integer> \n Currentyl, 1 block represents 200M. ")

        if(blocks > 0):
            eatFlag = True

            if(eatFlag):
                mem_to_eat = str(blocks * 0.2) + "GB" if (blocks>4) else str(blocks *200) + "MB"
                print("Will alocate about " + mem_to_eat +". Please use 'Ctrl+C' to Release." )
                try:
                    mem_list = []
                    for i in range(0,blocks):
                        mem_list.append(' ' * 1024 * 1024 * 200)
                    while(True):
                        time.sleep(1)
                except KeyboardInterrupt:
                    print("Will release the memory.")
        else:
            print("Usage: python eatmem.py <blocks to eat,should be an integer> \n  The specified blocks should not be lower than 1.")
    else:
        print(
            "Usage: python eatmem.py <blocks to eat,should be an integer> \n  eg: python eatmem.py 10 will allocate about 2G memory.")